﻿using System;
using System.Threading;
using System.Windows.Threading;

namespace WpfAppMissData.Extensions
{
    public static class WpfThreadingExtensions
    {
        /// <summary>
        /// Simple helper extension method to marshall to correct thread if its required
        /// 帮助控件扩展方法找到对应的线程
        /// </summary>
        /// <param name="control">原控件</param>
        /// <param name="methodCall">对应的Action</param>
        /// <param name="priorityForCall">The thread priority</param>
        public static void InvokeIfRequired(this DispatcherObject control, Action methodCall, DispatcherPriority priorityForCall)
        {
            //see if we need to Invoke call to Dispatcher thread  
            if (control.Dispatcher.Thread != Thread.CurrentThread)
                control.Dispatcher.Invoke(priorityForCall, methodCall);
            else
                methodCall();
        }
    }
}
